package com.zhoug.zmui3demo.ui;

import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.zhoug.common3.interfaces.NameTransformer;
import com.zhoug.common3.utils.AppUtils;
import com.zhoug.common3.utils.CollectionUtils;
import com.zhoug.zmui3.list.OnSelectChangeListener;
import com.zhoug.zmui3.list.SimpleSelectListAdapter;
import com.zhoug.zmui3.list.ZMUISelectionListDialog;
import com.zhoug.zmui3demo.R;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author: zhoug
 * @Date: 2024-07-05
 * @Description:
 */
public class SelectListActivity extends AppCompatActivity {
    private SimpleSelectListAdapter<Bean> listAdapter;
    private ZMUISelectionListDialog<Bean> mSelectionDialog;
    private Button btnClear;
    private Switch switchMulti;
    private Switch switchIcon;
    private Switch switchAlign;
    private TextView tvSelect;
    private RecyclerView recycleView;
    private Button btnSelectionDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_list);
        findViews();
        init();

    }

    private void init() {
        btnClear.setOnClickListener(v->{
            mSelectionDialog.clearSelect();
            listAdapter.clearSelect();
            listAdapter.notifyDataChange(true);
        });
        switchMulti.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mSelectionDialog.setMultiSelect(isChecked);
            listAdapter.setMultiSelect(isChecked);
            listAdapter.notifyDataChange(true);

        });
        switchIcon.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mSelectionDialog.showSelectImage(isChecked);
            listAdapter.showSelectImage(isChecked);
            listAdapter.notifyDataChange();

        });
        switchAlign.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked){
                mSelectionDialog.setTextAlign(Gravity.CENTER);
                listAdapter.setTextAlign(Gravity.CENTER);
            }else{
                mSelectionDialog.setTextAlign(Gravity.START);
                listAdapter.setTextAlign(Gravity.START);
            }
            listAdapter.notifyDataChange();
        });

        //选择窗口
        mSelectionDialog=new ZMUISelectionListDialog<Bean>(this) {
            @Override
            public String getItemName(Bean itemData) {
                return itemData.getName();
            }
        };
        mSelectionDialog.setMaxHeight(AppUtils.getScreenSize().y/2);
//        mSelectionDialog.setData(getData());
        mSelectionDialog.setOnSelectChangeListener(onSelectChangeListener);
        mSelectionDialog.setReLoadButton(v->{
            loadData();
        } );
        btnSelectionDialog.setOnClickListener(v->{
            mSelectionDialog.show();
            if(mSelectionDialog.getData().isEmpty()){
                loadData();
            }

        });

        listAdapter = new SimpleSelectListAdapter<Bean>() {
            @Override
            public String getItemName(Bean itemData) {
                return itemData.getName();
            }
        };
        listAdapter.setData(getData());
        listAdapter.setOnSelectChangeListener(onSelectChangeListener);
        recycleView.setAdapter(listAdapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));

    }

    private OnSelectChangeListener<Bean> onSelectChangeListener=new OnSelectChangeListener<Bean>() {
        @Override
        public void onSelectChange(@NonNull List<Bean> select) {
            String s = CollectionUtils.listToNames(select, ",");
            tvSelect.setText(s);
        }

        @Override
        public void onClearSelect() {
            tvSelect.setText("清空选择");

        }
    };

    private void loadData(){
        mSelectionDialog.showLoading(true);
        btnSelectionDialog.postDelayed(()->{
            Random random=new SecureRandom();
            if(random.nextBoolean()){
                mSelectionDialog.loadSuccess(getData());
            }else{
                mSelectionDialog.loadError("数据加载失败");
            }

        },3000);
    }

    private void findViews() {
        btnClear = findViewById(R.id.btn_clear);
        switchMulti = findViewById(R.id.switch_multi);
        switchIcon = findViewById(R.id.switch_icon);
        switchAlign = findViewById(R.id.switch_align);
        tvSelect = findViewById(R.id.tv_select);
        recycleView = findViewById(R.id.recycleView);
        btnSelectionDialog = findViewById(R.id.btn_selection_dialog);
    }

    private class Bean implements NameTransformer {
        private String id;
        private String name;

        public Bean(String name) {
            this.name = name;
            id = UUID.randomUUID().toString();
        }

        public Bean(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String transformName() {
            return name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Bean bean = (Bean) o;
            return Objects.equals(id, bean.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }


    private List<Bean> getData() {
        List<Bean> list = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            list.add(new Bean("Item" + i));
        }
        return list;
    }


    private SelectListActivity self(){
        return this;
    }
}
